#!/usr/bin/php -nq
<?php
/*
 *  avisaro wlan modul 2.0 command line tools
 *  Copyright (C) 2009 Daniel Werner, Mark Schmale
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once './simpleSocket.php';
require_once './avisaro.php';
require_once './cliParser.php';

echo  'avisaro wlan modul 2.0 command line tools  Copyright (C) 2009 Daniel Werner, Mark Schmale'.PHP_EOL
     .'This program comes with ABSOLUTELY NO WARRANTY;'.PHP_EOL
     .'This is free software, and you are welcome to redistribute it'.PHP_EOL
     .'under certain conditions; type "license" for details.'.PHP_EOL
     .'Last change: '.date("Y-m-d H:i", filemtime(__FILE__)).PHP_EOL;
if(count($argv) < 1) 
{
    echo  'Usage: '.$argv[0].' operation [params]'.PHP_EOL;
    die;
}

include '.avisaro/settings.php';
//////
// TODO: connect only if ip & port is given 
$con  = new avisaro($ip, $port);
echo 'connected'.PHP_EOL;
switch($argv[1]) 
{
	case 'run':
		$con->runScript();
		break;
	case 'upload':
		if(isset($argv[2]) && file_exists($argv[2])) {
			$con->loadScript($argv[2]);
		} else {
			die('File not found'.$argv[2].PHP_EOL);
		}
		break;
	case 'version':
		echo 'avisaro WLAN Modul 2.0 - Firmware: '.$con->getVer().PHP_EOL;
		break;
	case 'cli':
        $con->cliStart();
		readline_write_history(".avisaro/history");
		break;
	default:
		die('Unknown operation: '.$argv[1].PHP_EOL);
}
?>
